package g11.dao;

import g11.mis.VisitationContentAttributeEnum;
import g11.model.Visitation;
import g11.model.VisitationContent;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class VisitationContentDAOImpl implements VisitationContentDAO {

	private Session session;

	public Session getSession() {
		return this.session;
	}

	public void cleanup() {
		if (session.isOpen())
			session.close();
		// HibernateUtil.shutdown();
	}

	public VisitationContentDAOImpl() {

	}

	public Integer createVisitationContent(VisitationContent visitationContent)
			throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Integer visitationContentId;
		Transaction tran = null;
		try {

			tran = session.beginTransaction();
			visitationContentId = (Integer) session.save(visitationContent);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return visitationContentId;
	}

	public VisitationContent getCurrentVisitationContent(Visitation visitation)
			throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		VisitationContent visitationContent = new VisitationContent();
		try {
			System.out
					.println("get the Latest VisitationContent by Visitation "
							+ visitation.getVisitationId() + " version:"
							+ visitation.getCurrentVersion());
			String hql = "from VisitationContent vc where vc.visitation.visitationId=? and vc.version=?";
			Query q = session.createQuery(hql);
			q.setInteger(0, visitation.getVisitationId());
			q.setInteger(1, visitation.getCurrentVersion());
			if (q.list().size() > 0) {
				visitationContent = (VisitationContent) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return visitationContent;
	}

	public List<VisitationContent> getVisitationContentByVisitation(
			Visitation visitation) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		List<VisitationContent> visitationContentList = new ArrayList<VisitationContent>();
		try {
			String hql = "from VisitationContent vc where vc.visitation.visitationId=?";
			Query q = session.createQuery(hql);
			q.setInteger(0, visitation.getVisitationId());
			if (q.list().size() > 0) {
				visitationContentList = (List<VisitationContent>) q.list();
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return visitationContentList;
	}

	public int getLatestVersionOfVisitationContent(Visitation visitation)
			throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		int maxVersion = -1;
		try {
			String hql = "select max(vc.version) from VisitationContent vc where vc.visitation.visitationId = ?";
			Query q = session.createQuery(hql);
			q.setInteger(0, visitation.getVisitationId());
			if (q.list().size() > 0) {
				maxVersion = (Integer) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return maxVersion;
	}

	/*
	 * public void deleteVisitationContentByVisitation(Visitation visitation)
	 * throws DAOException { this.session =
	 * HibernateUtil.getSessionFactory().openSession(); List<VisitationContent>
	 * visitationContentList = new ArrayList<VisitationContent>(); try {
	 * visitation.getVisitationId()); String hql =
	 * "from VisitationContent vc where vc.visitation.visitationId=?"; Query q =
	 * session.createQuery(hql); q.setInteger(0, visitation.getVisitationId());
	 * if (q.list().size() > 0) { visitationContentList =
	 * (List<VisitationContent>) q.list();
	 * 
	 * for (int i = 0; i < visitationContentList.size(); i++) { Transaction tran
	 * = null; tran = session.beginTransaction(); // session.flush();
	 * session.delete(visitationContentList.get(i)); tran.commit(); } } } catch
	 * (HibernateException e) {
	 * 
	 * throw new DAOException(e.getMessage()); } finally { if (session.isOpen())
	 * session.close(); } }
	 */

	public void deleteVisitationContent(VisitationContent visitationContent)
			throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			// session.flush();
			// visitationContent = (VisitationContent) session
			// .merge(visitationContent);
			session.delete(visitationContent);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

	}

	/*
	 * public Visitation createVisitationContentWithVersion( VisitationContent
	 * visitationContent) throws DAOException { this.session =
	 * HibernateUtil.getSessionFactory().openSession(); Visitation
	 * returnVisitation = new Visitation(); try {
	 * String hql =
	 * "from VisitationContent vc where vc.visitation.visitationId=?"; Query q =
	 * session.createQuery(hql); q.setInteger(0,
	 * visitationContent.getVisitation().getVisitationId()); Integer
	 * visitationContentId; if (q.list().size() > 0) { // if the
	 * visitationcontent already // existed, ++the latestversion and // create a
	 * new entry. int latestVersion = this
	 * .getLatestVersionOfVisitationContent(visitationContent .getVisitation());
	 * visitationContent.setVersion(latestVersion + 1); visitationContentId =
	 * this .createVisitationContent(visitationContent); } else { // if the
	 * visitationcontent does not exist, using the // default version
	 * number(generally "1" from visitation) // and create a new entry.
	 * visitationContentId = this .createVisitationContent(visitationContent); }
	 * returnVisitation.setVisitationId(visitationContent.getVisitation()
	 * .getVisitationId());
	 * 
	 * // persist the prescriptions if there is any contained in the //
	 * VisitationContent
	 * returnVisitation.setCurrentVersion(visitationContent.getVersion()); }
	 * catch (HibernateException e) { throw new DAOException(e.getMessage()); }
	 * finally { if (session.isOpen()) session.close(); } return
	 * returnVisitation; }
	 */

	@Override
	public List<VisitationContent> getVisitationContentByKeyword(
			String keyword, int attribute) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		List<VisitationContent> visitationContentList = new ArrayList<VisitationContent>();
		VisitationContentAttributeEnum vcaEnum = null;
		try {
			String hql = null;
			if (attribute == 0) {
				hql = "from VisitationContent vc where vc.diagnosis like :keyw";
			} else if (attribute == 1) {
				hql = "from VisitationContent vc where vc.treatment like :keyw";
			} else if (attribute == 2) {
				hql = "from VisitationContent vc where vc.comment like :keyw";
			} else if (attribute == 3) {
				hql = "from VisitationContent vc where vc.visitation.appointment.patient.name like :keyw";
			}
			Query q = session.createQuery(hql);
			q.setString("keyw", "%" + keyword + "%");
			if (q.list().size() > 0) {
				visitationContentList = (List<VisitationContent>) q.list();
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

		return visitationContentList;
	}

	@Override
	public VisitationContent getVisitationContentById(
			Integer visitationContentId) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		VisitationContent visitationContent = new VisitationContent();
		try {
			System.out
					.println("get the VisitationContent by VisitationContentID "
							+ visitationContentId);
			String hql = "from VisitationContent vc where vc.visitationContentId =?";
			Query q = session.createQuery(hql);
			q.setInteger(0, visitationContentId);
			if (q.list().size() > 0) {
				visitationContent = (VisitationContent) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return visitationContent;
	}
}
